ringbuf
Lock-free single-producer single-consumer (SPSC) FIFO ring buffer with direct access to inner data.
Overview
RingBuffer
is the initial structure representing ring buffer itself.
Ring buffer can be splitted into pair of Producer
and Consumer
.
Producer
and Consumer
are used to append/remove elements to/from the ring buffer accordingly. They can be safely transfered between threads.
Operations with Producer
and Consumer
are lock-free - they're succeded or failed immediately without blocking or waiting.
Elements can be effectively appended/removed one by one or many at once.
Also data could be loaded/stored directly into/from Read
/Write
instances.
And finally, there are unsafe
methods allowing thread-safe direct access in place to the inner memory being appended/removed.
When building with nightly toolchain it is possible to run benchmarks via cargo bench
.
Documentation
Examples
Simple example
use ;
let rb = new;
let = rb.split;
prod.push.unwrap;
prod.push.unwrap;
assert_eq!;
assert_eq!;
prod.push.unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
Message transfer
This is more complicated example of transfering text message between threads.
use ;
use thread;
use ;
use ;
let rb = new;
let = rb.split;
let smsg = "The quick brown fox jumps over the lazy dog";
let pjh = spawn;
let cjh = spawn;
pjh.join.unwrap;
let rmsg = cjh.join.unwrap;
assert_eq!;
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.